<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>组件细节2</title>
  <script src="./vue.js"></script>
</head>
<body>
  <div id="root">
    <counter ref="one" @change="handleChange"></counter>
    <counter ref="two" @change="handleChange"></counter>
    <div>{{total}}</div>
  </div>
  
  <script>
    Vue.component('counter', { // 求和计算器 点击子组件，数字递增 total求和
      template: '<div @click="handleClick">{{number}}</div>',
      data: function() {
        return {
          number: 0
        }
      },
      methods: {
        handleClick: function() {
          this.number ++;
          this.$emit('change'); // 子组件向父组件触发事件传值
        }
      }
    })
    var vm = new Vue({
      el: '#root',
      data: {
        total: 0
      },
      methods: {
        handleChange: function() {
          // alert('change')
          this.total = this.$refs.one.number + this.$refs.two.number
        }
      }
    })
  </script>
</body>
</html>